<template>
  <div class="login" :style="{ height: pageHeight + 'px' }">
    <yk-space style="display: flex;align-items: center;" dir="vertical" :size="24" class="login-container">
      <yk-space dir="vertical" class="title-container">
        <img src="../assets/LOGO.svg" class="logo">
        <span class="name">博客管理员后台注册</span>
      </yk-space>
     
      <form @submit.prevent="handleRegister" autocomplete="on">
        <yk-space style="display: flex;align-items: center;" dir="vertical" :size="24">    
           <input 
           placeholder="请输入注册密钥" 
           autocomplete="registerCode" 
           name="registerCode"
           v-model="registerCode"
           required
           inputmode="text"></input>    
            <input 
            placeholder="请输入用户名" 
            v-model="username" 
            required
            autocomplete="username"
            name="username"
            inputmode="text"
          ></input>
          <input 
            placeholder="请输入邮箱" 
            v-model="email" 
            prefix-icon="mail" 
            required
            autocomplete="email"
            name="email"
            inputmode="email"
          ></input>
          <input 
            placeholder="请输入密码" 
            v-model="password" 
            type="password" 
            required
            autocomplete="new-password"
            name="password"
            inputmode="text"
          ></input>
          <input 
            placeholder="请确认密码" 
            v-model="confirmPassword" 
            type="password"
            required
            autocomplete="new-password"
            name="confirmPassword"
            inputmode="text"
          ></input>
          <div style="width: 70%; display: flex; justify-content: space-between;">
             <yk-button type="primary" @click="goBack" block>返回登录</yk-button>
            <yk-button type="primary" native-type="submit" block>提交注册</yk-button>
          </div>
        </yk-space>
      </form>
    </yk-space>
  </div>
</template>

<script setup lang="ts">
import { ref, getCurrentInstance } from 'vue';
import { useRouter } from 'vue-router';
import { RegisterApi } from '../api/user_api.ts';
import { useCode } from '../hooks/codeHook.ts';
import { getTimeYMDHMS } from '../utils/time.ts';

const router = useRouter();
const { tackleCode } = useCode();
const proxy: any = getCurrentInstance()?.proxy;

const registerCode = ref('');
const pageHeight = ref(window.innerHeight);
const username = ref('');
const email = ref('');
const password = ref('');
const confirmPassword = ref('');
const goBack = () => {
  router.push('/login');
}


// 更新为表单提交处理
const handleRegister = () => {
  // 邮箱格式验证
  const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
  if (!emailRegex.test(email.value.trim())) {
    proxy.$message({ type: 'error', message: '请输入有效的邮箱地址' });
    return;
  }
  // 密码长度验证
  if (password.value.length < 8) {
    proxy.$message({ type: 'error', message: '密码长度不能少于8位' });
    return;
  }
  if (!password.value.trim()) {
    proxy.$message({ type: 'error', message: '密码不能为空' });
    return;
  }
  if (password.value !== confirmPassword.value) {
    proxy.$message({ type: 'error', message: '两次密码输入不一致' });
    return;
  }
  // 注册密钥验证
  if (registerCode.value !== '123456') {
    console.log(registerCode.value);
    proxy.$message({ type: 'error', message: '注册密钥错误' });
    return;
  }

  let data = {
    user_name: username.value,
    email: email.value,
    password: password.value,
    created_at: getTimeYMDHMS(new Date()),
  }
  RegisterApi(data).then((res: any) => {
    if (tackleCode(res.data.code)) {
      //注册成功
      proxy.$message({ type: 'success', message: '注册成功' });
      router.push('/login');
    }
    else {
      //注册失败
      proxy.$message({ type: 'error', message: '输入不完整' });
    }
  })
};
</script>

<style scoped lang="less">
.login {
  z-index: 1000;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  width: 100%;
  min-height: 100vh;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  position: fixed;
  top: 0;
  left: 0;
}

.login-container {
  width: 420px;
  padding: 48px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.15);
  backdrop-filter: blur(10px);
  animation: slideUp 0.6s ease-out;
}

.title-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
}

.logo {
  width: 100px;
  height: 100px;
  filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.1));
}

.name {
  font-size: 28px;
  font-weight: 700;
  color: #2d3748;
  margin: 0;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

form {
  width: 100%;
}

input {
  width: 70%;
  height: 40px;
  padding: 0 @space-l;
  background-color:@bg-color-l;
  color:  @font-color-l;
  border-radius: 12px;
  border: 2px solid #e2e8f0;
  transition: all 0.3s ease;
  &:focus-within {
    border-color: #667eea;
  }
  
  &:hover {
    border-color: #667eea;
  }
}

.yk-button[type="primary"] {
  border-radius: 12px;
  height: 48px;
  font-weight: 600;
  font-size: 16px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  transition: all 0.3s ease;
  
  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 25px rgba(102, 126, 234, 0.3);
  }
  
  &:active {
    transform: translateY(0);
  }
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 480px) {
  .login-container {
    width: 90%;
    padding: 32px 24px;
    margin: 20px;
  }
  
  .name {
    font-size: 24px;
  }
}
</style>